import axios from './index'
import { apiUrl } from '../config'

// const token = localStorage.getItem('token');
const token = ''
console.log(token)

class Services {
  // get请求
  get (api, params) {
    return axios.get(api, {
      params: params,
      headers: {
        token: token
      }
    })
  }

  // post请求
  post (api, params) {
    return axios({
      method: 'post',
      url: apiUrl + api,
      data: params,
      transformRequest: [function (data) {
        let ret = ''
        for (const it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: token
      }
    })
  }

  // 上传接口/提交表单
  upload (api, params) {
    return axios({
      method: 'post',
      url: apiUrl + api,
      data: params,
      headers: {
        'Content-Type': 'multipart/form-data',
        token: token
      },
      withCredentials: true
    })
  }

  // 登录专用
  loginPost (api, params) {
    return axios({
      method: 'post',
      url: apiUrl + api,
      data: params,
      transformRequest: [function (data) {
        let ret = ''
        for (const it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
  }
}
export default new Services()
